package top.meijhm.communitymanagement.user.repository;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.rest.core.annotation.RestResource;
import org.springframework.stereotype.Repository;
import top.meijhm.communitymanagement.user.model.User;
import top.meijhm.communitymanagement.user.model.UserRole;

import javax.transaction.Transactional;
import java.util.List;

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
    User findByUsernameAndDeleted(String username, Boolean deleted);

    User findByIdAndDeleted(Long id, Boolean deleted);

    User findByUsername(String username);

    List<User> findAllByUserRoleAndDeleted(UserRole userRole, Boolean deleted);

    List<User> findAllByDeleted(Boolean deleted);

    @RestResource(exported = false)
    Page<User> findAllByDeleted(Pageable pageable, Boolean deleted);

    @Transactional
    @Modifying
    @Query("delete from User where username = (:username)")
    void dropUserByName(String username);
}